﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ALDIOrganizer
{
    public partial class formAboutProgram : Form
    {
        public formAboutProgram()
        {
            InitializeComponent();        
        }

        static void HandleException(string pSorry, Exception pException)
        {
            MessageBox.Show(pSorry
                + Environment.NewLine
                + pException.Message,
                "Error",
                MessageBoxButtons.OK,
                MessageBoxIcon.Error);
        }

        private void formAboutProgram_Load(object sender, EventArgs e)
        {
            var controls = (from object control in gbxAuthor.Controls select control as Control).Concat(
                from object control in gbxAcknowledgements.Controls select control as Control).ToList();
            foreach (var control in controls)
            {
                if (!(control is LinkLabel))
                {
                    continue; // !!! if we're not dealing with a LinkLabel, we simply jump to the next control
                }
                // so the rest of the code is safe
                
                var llabel = control as LinkLabel;

                #region automatically opening an email client
                if (llabel.Text.Contains(@"@"))
                {
                    llabel.LinkClicked += (_sender, _e) =>
                                              {
                                                  try
                                                  {
                                                      Process.Start(@"mailto:" + llabel.Text);
                                                  }
                                                  catch (Exception _ex)
                                                  {
                                                      Program.LogError(String.Empty, _ex);
                                                      HandleException(
                                                          "Sorry - I tried to open your default web mail client for you, but the following exception ocurred: ",
                                                          _ex);
                                                  }
                                              };
                }
                #endregion

                #region automatically opening default web browser
                else
                {
                    llabel.Links.Clear();
                    llabel.Links.Add(0, llabel.Text.Length, llabel.Text);
                    llabel.LinkClicked += (_sender, _e) =>
                                              {
                                                  try
                                                  {
                                                      var link = _e.Link.LinkData.ToString();
                                                      var sInfo = new ProcessStartInfo(
                                                          (link.Contains("http://") ? String.Empty : "http://") +
                                                          link);
                                                      Process.Start(sInfo);
                                                  }
                                                  catch (Win32Exception ex32)
                                                  {
                                                      // get over it

                                                      /* note: there's always that 'file not found' win32 exception, even though
                                                       * it does open the link eventually - first thought try and set UseShellExecute
                                                       * to false, well it doesn't work, tried to google a solution out - other than
                                                       * retrieving the default browser from registry then launching it explicitly -
                                                       * no fix found, so i just leave it like that for now */
                                                  }
                                                  catch (Exception otherEx)
                                                  {
                                                      HandleException(
                                                          "Sorry - I tried to open the link in your default web browser, but the following exception ocurred: ",
                                                          otherEx);
                                                  }
                                              };
                }
                #endregion
            }
        }

        
        
    }
}
